package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.OtherHouseInfo;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.OtherHouseInfoRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/09
 * @Description: 其他房屋基础资料报表
 */
//@Service("其他房屋基础资料报表")
@Service("12")
public class OtherHouseInfoServiceImpl implements StrategyService {
    @Resource
    OtherHouseInfoRepository otherHouseInfoRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;

    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId, ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            otherHouseInfoRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }
        List<OtherHouseInfo> list = getList(workbook,xlrmId);
        otherHouseInfoRepository.batchSave(list);
        reportPublicField.setProvince(list.get(0).getProvince());
        reportPublicField.setCity(list.get(0).getCity());
        reportPublicField.setCounty(list.get(0).getCounty());
        //默认删除
        reportPublicField.setDelFlag(YNEnum.Y.toString());
        reportPublicFieldRepository.save(reportPublicField);
    }

    private List<OtherHouseInfo>getList(Workbook workbook,String xlrmId){
        List<OtherHouseInfo> dataLst = new ArrayList<>();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            //填表人
            String fillFormUser = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(1));
            //联系电话
            String tel = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(3));
            //填表日期
            String fillFormTime = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(5));
            //省
            String province = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(1));
            //市
            String city = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(3));
            //县
            String district = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(5));
            //单价
            String unitPrice1 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(5));
            String unitPrice2 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(6));
            String unitPrice31 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(7));
            String unitPrice32 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(8));
            String unitPrice4 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(9));
            String unitPrice51 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(10));
            String unitPrice52 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(11));
            String unitPrice53 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(12));
            String unitPrice6 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(13));
            String unitPrice7 = hSSFDataFormatter.formatCellValue(sheet.getRow(5).getCell(14));
            /** 循环Excel的行 从第七行开始*/
            for (int r = 6; r < totalRows; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                String s = hSSFDataFormatter.formatCellValue(row.getCell(1));
                if(s.equals("")){
                    continue;
                }
                OtherHouseInfo obj = new OtherHouseInfo();
                //主键id
                obj.setXgoiId(UUID.randomUUID().toString().replaceAll("-", ""));
                //报表主表id
                obj.setXlrmId(xlrmId);
                //填表人
                obj.setFillFormUser(fillFormUser);
                //tel
                obj.setTel(tel);
                //填报日期
                obj.setFillFormTime(fillFormTime);
                //省
                obj.setProvince(province);
                //市
                obj.setCity(city);
                //县
                obj.setCounty(district);
                //单价
                obj.setUnitPrice1(unitPrice1);
                obj.setUnitPrice2(unitPrice2);
                obj.setUnitPrice31(unitPrice31);
                obj.setUnitPrice32(unitPrice32);
                obj.setUnitPrice4(unitPrice4);
                obj.setUnitPrice51(unitPrice51);
                obj.setUnitPrice52(unitPrice52);
                obj.setUnitPrice53(unitPrice53);
                obj.setUnitPrice6(unitPrice6);
                obj.setUnitPrice7(unitPrice7);
                //I类
                obj.setArea1(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                obj.setArea2(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                obj.setArea31(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                obj.setArea32(hSSFDataFormatter.formatCellValue(row.getCell(8)));
                obj.setArea4(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                obj.setArea51(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                obj.setArea52(hSSFDataFormatter.formatCellValue(row.getCell(11)));
                obj.setArea53(hSSFDataFormatter.formatCellValue(row.getCell(12)));
                obj.setArea6(hSSFDataFormatter.formatCellValue(row.getCell(13)));
                obj.setArea7(hSSFDataFormatter.formatCellValue(row.getCell(14)));
                //行政单位
                obj.setUnitName(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                //用房所属单位
                obj.setAffiliatedUnit(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                //用途
                obj.setFun(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                //平均室内财产
                obj.setIndoorAssetAvg(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                dataLst.add(obj);
            }
        }
        return dataLst;
    }
}
